package com.jwaoo.mobile;
import android.util.Log;
import com.dx.as.JWCache;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.log.LogUtil;
import com.dx.as.common.storage.StorageUtil;
import com.dx.as.common.sys.ScreenUtil;
import com.jwaoo.mobile.common.crash.RunTimeCrashHandler;
import com.jwaoo.mobile.common.listener.ActivityLifecycleListener;
import com.jwaoo.mobile.common.listener.CrashHandlerLisener;
import com.jwaoo.mobile.common.listener.LogListener;
import com.jwaoo.mobile.forchat.IFroChatMouleImpl;
import com.jwaoo.mobile.http.HttpHelper;
import java.util.List;
import java.util.Map;
import http.IHttpListener;
import image.ImageLoaderKit;

/**
 * Created by jwaoo on 2017/6/15.
 */

public class ApplicationHelper {
    private static Application mApplication;
    private static ActivityLifecycleListener lifecycleListener;
    public static Class<?> lastCls;
    public static void init(Application application){
        mApplication = application;
        initTools();
        getAllUrl();
        initCrashHandlerLisener();
        registerUiListener();
        IFroChatMouleImpl.getInstance().initChatModule();
        JWCache.activityList = lifecycleListener.getActivityList();
    }

    public static void initTools(){
        ScreenUtil.init(mApplication);
//        if (!StringUtil.isEmpty(CommonUtils.getToken())) {
            StorageUtil.init(mApplication,CommonUtils.getBaseDir(mApplication));
            new ImageLoaderKit(mApplication,null);
            LogUtil.init(CommonUtils.getLogDir(mApplication),Log.ERROR,new LogListener(mApplication));
//        }
    }

    public static void initCrashHandlerLisener(){
        RunTimeCrashHandler.getInstance().init(mApplication,new  CrashHandlerLisener());
    }

    public static boolean isCurentActvity(Class<?> cls){
        return lifecycleListener.isCurrentActivity(cls);
    }

    private static void registerUiListener(){
        lifecycleListener = new ActivityLifecycleListener(mApplication);
        mApplication.registerActivityLifecycleCallbacks(lifecycleListener);
    }

    public static boolean isActivityStarted(Class <?> cls ){
        List<Class<?>> list = lifecycleListener.getActivityList(); 
        if (list != null) {
            return list.contains(cls);
        }
        return false;
    }

    public static Class<?> getCurrentActivity(){
        if (lifecycleListener.getActivityList().isEmpty()){
            return null;
        }else {
            return lifecycleListener.getActivityList().get(lifecycleListener.getActivityList().size()-1);
        }
    }

    private static void getAllUrl(){
        HttpHelper.getUrl(new IHttpListener() {
            @Override
            public void onResponse(Object response, int flg) {
                if (response == null) return;
                Map<String,Object> result = (Map<String,Object>)response;
                if (result.containsKey("file"))
                    CommonUtils.saveFileUrl(result.get("file").toString());
                if (result.containsKey("webrtc")){
                    String rtcUrl = result.get("webrtc").toString();
                    CommonUtils.saveRtcUrl(rtcUrl);
                }
                if (result.containsKey("chat"))
                    CommonUtils.saveChatUrl(result.get("chat").toString());
            }
            @Override
            public void onFailure(Throwable t) {

            }
        });
    }

    public static boolean isBackground(){
        return lifecycleListener.isBackground();
    }

}
